package com.ctrip.cap.client.impl;

import java.io.UnsupportedEncodingException;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctrip.cap.client.JiraApiClient;
import com.ctrip.cap.client.property.CapClientProperties;
import com.ctrip.cap.domain.jira.IssueFields;
import com.ctrip.cap.domain.jira.IssueInfo;
import com.ctrip.cap.domain.jira.IssueType;
import com.ctrip.cap.domain.jira.JiraAssignee;
import com.ctrip.cap.domain.jira.JiraProject;
import com.ctrip.cap.domain.jira.JiraUser;
import com.ctrip.cap.exception.CapDataException;

public class JiraApiClientImpl implements JiraApiClient {

	private static final Logger logger = LoggerFactory
			.getLogger(JiraApiClientImpl.class);
	
	private static JiraApiClientImpl client = new JiraApiClientImpl();
	
	private JiraApiClientImpl(){
		super();
	}
	
	public static JiraApiClientImpl getInstance(){
		return client;
	}
	
	@Override
	public void ticketJiraIssue(IssueInfo issue, JiraUser user) throws UnsupportedEncodingException{


		
		Client client = ClientBuilder.newClient();
		WebTarget webTarget = client
				.target(CapClientProperties.JIRA_TICKET_ISSUE_URL);
		Invocation.Builder invocationBuilder = webTarget.request();
		Response response = invocationBuilder.header("Authorization", "Basic "+getJiraToken(user)).post(Entity.entity(issue, MediaType.APPLICATION_JSON));

		String resString = response.readEntity(String.class);
		
		if (response.getStatus() != 200&&response.getStatus() != 201) {
			throw new CapDataException(resString);
		}
		
	}
	
	public String getJiraToken(JiraUser user) throws UnsupportedEncodingException{
		String base64 = new String(Base64.encodeBase64((user.getName()+":"+user.getPwd()).getBytes()),
				CapClientProperties.DEFAULT_ENCODING);
		logger.info("Base64 encoded nameAndPwdPair is " + base64);
		return base64;
	}
	
	public static void main(String args[]){
		JiraApiClient jiraClient = new JiraApiClientImpl();
		try {
			JiraProject jiraProject = new JiraProject();
			jiraProject.setKey("FX23AUT");
			
			IssueType issueType = new IssueType();
			issueType.setName("Bug");
			
			JiraAssignee jiraAssignee = new JiraAssignee();
			jiraAssignee.setName("c.shi");
			
			IssueFields fields = new IssueFields();
			fields.setProject(jiraProject);
			fields.setIssuetype(issueType);
			fields.setSummary("summary test");
			fields.setDescription("description test");
			fields.setAssignee(jiraAssignee);
			
			IssueInfo issue = new IssueInfo();
			issue.setFields(fields);
			
			JiraUser user = new JiraUser();
			user.setName("tfsci");
			user.setPwd("Tfs_ci12345");
			
			jiraClient.ticketJiraIssue(issue, user);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	

}
